﻿var t = require("../../api.js"), a = getApp(), e = !1, i = !1;

var app = getApp();
var ut = require("../../utils.js");
var wp = require("../../wxParse/wxParse.js");


Page({
    data: {
        cat_id: "",
      form: {
        number: 1
      },
      show_attr_picker: !1,
      id: null,
        page: 1,
        cat_list: [],
        goods_list: [],
        sort: 0,
        sort_type: -1
    },
  addCart(e) {
    this.setData({
      id: e.currentTarget.dataset.id
    })
    this.getGoods();

  },
  goods_recommend: function (a) {
    var i = this;
    i.setData({
      is_loading: !0
    });
    var r = i.data.page || 2;
    app.request({
      url: t.default.goods_recommend,
      data: {
        goods_id: a.goods_id,
        page: r
      },
      success: function (t) {
        if (0 == t.code) {
          if (a.reload) o = t.data.list;
          if (a.loadmore) var o = i.data.goods_list.concat(t.data.list);
          i.data.drop = !0, i.setData({
            goods_list: o
          }), i.setData({
            page: r + 1
          });
        }
      },
      complete: function () {
        i.setData({
          is_loading: !1
        });
      }
    });
  },
  getGoods: function () {
    var that = this;
    app.request({
      url: t.default.goods,
      data: {
        id: that.data.id
      },
      success: function (t) {
        console.log(t)
        if (0 == t.code) {
          var o = t.data.detail;
          wp.wxParse("detail", "html", o, that), that.setData({
            goods: t.data,
            attr_group_list: t.data.attr_group_list
          }), that.goods_recommend({
            goods_id: t.data.id,
            reload: !0
          });
          that.data.goods.miaosha;
          that.selectDefaultAttr();

        }
        1 == t.code && wx.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function (t) {
            t.confirm && wx.switchTab({
              url: "/pages/index/index"
            });
          }
        });
      }
    });
  },
  selectDefaultAttr: function () {
    var that = this;
    if (that.data.goods && 0 === that.data.goods.use_attr) {
      for (var mt in that.data.attr_group_list) for (var o in that.data.attr_group_list[mt].attr_list) 0 == mt && 0 == o && (that.data.attr_group_list[mt].attr_list[o].checked = !0);
      that.setData({
        attr_group_list: that.data.attr_group_list
      });
    }
    console.log("ss")
    that.submit("ADD_CART");
  },
  submit: function (a) {

    var i = this;
    console.log("s2")
    // if (!i.data.show_attr_picker) return i.setData({
    //   show_attr_picker: !0
    // }), !0;


    i.setData({
      show_attr_picker: !0
    });
    if (i.data.miaosha_data && i.data.miaosha_data.rest_num > 0 && i.data.form.number > i.data.miaosha_data.rest_num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    if (i.data.form.number > i.data.goods.num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    console.log(i.data.attr_group_list)
    var r = i.data.attr_group_list, e = [];
    for (var s in r) {
      var d = !1;
      for (var n in r[s].attr_list) if (r[s].attr_list[n].checked) {
        d = {
          attr_id: r[s].attr_list[n].attr_id,
          attr_name: r[s].attr_list[n].attr_name
        };
        console.log(d)
        break;
      }
      console.log(d)
      if (!d) return wx.showToast({
        title: "请选择" + r[s].attr_group_name,
        image: "/images/icon-warning.png"
      }), !0;
      e.push({
        attr_group_id: r[s].attr_group_id,
        attr_group_name: r[s].attr_group_name,
        attr_id: d.attr_id,
        attr_name: d.attr_name
      });
    }
    "ADD_CART" == a && (wx.showLoading({
      title: "正在提交",
      mask: !0
    }), app.request({
      url: t.cart.add_cart,
      method: "POST",
      data: {
        goods_id: i.data.id,
        attr: JSON.stringify(e),
        num: 1
      },
      success: function (t) {
        console.log(t)
        wx.showToast({
          title: t.msg,
          duration: 1500
        }), wx.hideLoading(), i.setData({
          show_attr_picker: !1
        });
      }
    }));
  },
    onLoad: function(t) {
        a.pageOnLoad(this), this.loadData(t);
    },
    loadData: function(t) {
        var a = this, e = wx.getStorageSync("cat_list"), i = "";
        if (t.cat_id) for (var s in e) {
            var o = !1;
            e[s].id == t.cat_id && (e[s].checked = !0, e[s].list.length > 0 && (i = "height-bar"));
            for (var d in e[s].list) e[s].list[d].id == t.cat_id && (e[s].list[d].checked = !0, 
            o = !0, i = "height-bar");
            o && (e[s].checked = !0);
        }
        if (t.goods_id) var r = t.goods_id;
        a.setData({
            cat_list: e,
            cat_id: t.cat_id || "",
            height_bar: i,
            goods_id: r || ""
        }), a.reloadGoodsList();
    },
    catClick: function(t) {
        var a = this, e = "", i = t.currentTarget.dataset.index, s = a.data.cat_list;
        for (var o in s) {
            for (var d in s[o].list) s[o].list[d].checked = !1;
            o == i ? (s[o].checked = !0, e = s[o].id) : s[o].checked = !1;
        }
        var r = "";
        s[i].list.length > 0 && (r = "height-bar"), a.setData({
            cat_list: s,
            cat_id: e,
            height_bar: r
        }), a.reloadGoodsList();
    },
    subCatClick: function(t) {
        var a = this, e = "", i = t.currentTarget.dataset.index, s = t.currentTarget.dataset.parentIndex, o = a.data.cat_list;
        for (var d in o) for (var r in o[d].list) d == s && r == i ? (o[d].list[r].checked = !0, 
        e = o[d].list[r].id) : o[d].list[r].checked = !1;
        a.setData({
            cat_list: o,
            cat_id: e
        }), a.reloadGoodsList();
    },
    allClick: function() {
        var t = this, a = t.data.cat_list;
        for (var e in a) {
            for (var i in a[e].list) a[e].list[i].checked = !1;
            a[e].checked = !1;
        }
        t.setData({
            cat_list: a,
            cat_id: "",
            height_bar: ""
        }), t.reloadGoodsList();
    },
    reloadGoodsList: function() {
        var e = this;
        i = !1, e.setData({
            page: 1,
            goods_list: [],
            show_no_data_tip: !1
        });
        var s = e.data.cat_id || "", o = e.data.page || 1;
        a.request({
            url: t.default.goods_list,
            data: {
                cat_id: s,
                page: o,
                sort: e.data.sort,
                sort_type: e.data.sort_type,
                goods_id: e.data.goods_id
            },
            success: function(t) {
                0 == t.code && (0 == t.data.list.length && (i = !0), e.setData({
                    page: o + 1
                }), e.setData({
                    goods_list: t.data.list
                })), e.setData({
                    show_no_data_tip: 0 == e.data.goods_list.length
                });
            },
            complete: function() {}
        });
    },
    loadMoreGoodsList: function() {
        var s = this;
        if (!e) {
            s.setData({
                show_loading_bar: !0
            }), e = !0;
            var o = s.data.cat_id || "", d = s.data.page || 2, r = s.data.goods_id;
            a.request({
                url: t.default.goods_list,
                data: {
                    page: d,
                    cat_id: o,
                    sort: s.data.sort,
                    sort_type: s.data.sort_type,
                    goods_id: r
                },
                success: function(t) {
                    0 == t.data.list.length && (i = !0);
                    var a = s.data.goods_list.concat(t.data.list);
                    s.setData({
                        goods_list: a,
                        page: d + 1
                    });
                },
                complete: function() {
                    e = !1, s.setData({
                        show_loading_bar: !1
                    });
                }
            });
        }
    },
    onReachBottom: function() {
        var t = this;
        i || t.loadMoreGoodsList();
    },
    onShow: function(t) {
        a.pageOnShow(this);
        var e = this;
        if (wx.getStorageSync("list_page_reload")) {
            var i = wx.getStorageSync("list_page_options");
            wx.removeStorageSync("list_page_options"), wx.removeStorageSync("list_page_reload");
            var s = i.cat_id || "";
            e.setData({
                cat_id: s
            });
            var o = e.data.cat_list;
            for (var d in o) {
                var r = !1;
                for (var c in o[d].list) o[d].list[c].id == s ? (o[d].list[c].checked = !0, r = !0) : o[d].list[c].checked = !1;
                r || s == o[d].id ? (o[d].checked = !0, o[d].list && o[d].list.length > 0 && e.setData({
                    height_bar: "height-bar"
                })) : o[d].checked = !1;
            }
            e.setData({
                cat_list: o
            }), e.reloadGoodsList();
        }
    },
    sortClick: function(t) {
        var a = this, e = t.currentTarget.dataset.sort, i = void 0 == t.currentTarget.dataset.default_sort_type ? -1 : t.currentTarget.dataset.default_sort_type, s = a.data.sort_type;
        if (a.data.sort == e) {
            if (-1 == i) return;
            s = -1 == a.data.sort_type ? i : 0 == s ? 1 : 0;
        } else s = i;
        a.setData({
            sort: e,
            sort_type: s
        }), a.reloadGoodsList();
    },
    onReady: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {}
});